package com.chenxi.service.impl;

import com.chenxi.VO.MenuVO;
import com.chenxi.mapper.MenuMapper;
import com.chenxi.model.Menu;
import com.chenxi.service.MenuServiceI;
import com.chenxi.util.CopyUtil;
import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("menuService")
public class MenuServiceImpl implements MenuServiceI{

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Menu getById(Long id) {
        return menuMapper.getById(id);
    }

    @Override
    public List<MenuVO> find(Map<String, Object> param) {
        List<Menu> list = menuMapper.find(param);
        List<MenuVO> ms = Lists.newArrayList();
        list.forEach(menu -> {
            Map<String,Object> map = Maps.newHashMap();
            map.put("parentId",menu.getId());
            List<Menu> subMenu = menuMapper.find(map);
            MenuVO mv = new MenuVO();
            CopyUtil.CopyBean(menu,mv);
            List<MenuVO> subls = Lists.newArrayList();
            subMenu.forEach(sm->{
                MenuVO smv = new MenuVO();
                CopyUtil.CopyBean(sm,smv);
                subls.add(smv);
            });
            mv.setSubList(subls);
            ms.add(mv);
        });
        return ms;
    }

    @Override
    public Page<Menu> page(Map<String, Object> param) {
        return null;//menuMapper.page(param);
    }

    @Override
    public void save(Menu m) {
        //menuMapper.save(m);
    }

    @Override
    public void update(Menu m) {
        //menuMapper.update(m);
    }

    @Override
    public void delete(Menu m) {
        menuMapper.delete(m);
    }
}
